package 异常.自定义异常;

public class Test {
    public static void main(String[] args) {
        // 目标: 掌握自定义异常, 以及异常的作用

        // 需求: 保存一个合法的年龄
        try {
            saveAge(160);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            saveAge2(123);
        } catch (AgeIllegalException e) {
            throw new RuntimeException(e);
        }
    }

    public static void saveAge(int age) {
        if (age > 0 && age < 150) {
            System.out.println("年龄被成功保存: " + age);
        } else {
            // 用一个异常对象来封装这个问题
            // throw 抛出去这个异常对象
            throw new AgeIllegalRuntimeException("age is illegal, you age is " + age);
        }
    }

    public static void saveAge2(int age) throws AgeIllegalException{
        if (age > 0 && age < 150) {
            System.out.println("年龄被成功保存: " + age);
        } else {
            // 用一个异常对象来封装这个问题
            // throw 抛出去这个异常对象
            throw new AgeIllegalException("age is illegal, you age is " + age);
        }
    }
}
